﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Threading;
using System.Globalization;
using Nivdal.AuxCode;

namespace Nivdal
{
    public partial class Friends : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = Resources.Resource.Default_PageTitle + ", " + Resources.Resource.Friends_PageTitle;
            // security part
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
                Response.Redirect("~/Default.aspx");
            
            OnlineUser ou = (OnlineUser)Session["User"];

            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();

            if (ou.TeamId == "Null")
                Response.Redirect("~/GeneralMessage.aspx?MessageString=M74ggdizz5wfeh");
            
            //removed - this page will be visible to all the users. Not only to premium users.
            //if (!ou.IsPremium)
            //    Response.Redirect("~/GeneralMessage.aspx?MessageString=gfkkjFSDei3w7lzTD");

            if (Request.QueryString["Message"] != null)
                this.MessageLabel.Text = Request.QueryString["Message"];

            this.MessagePanel.Visible = (this.MessageLabel.Text.Length > 0);

            if (!IsPostBack)
            {
                this.SuggestionsDataSourse.SelectParameters.Add("UserId", ou.UserId);
                this.MySuggestionsDataSourse.SelectParameters.Add("UserId", ou.UserId);

                // deal with niewbie tips                
                if ((int)Session["NewbieTipStage"] == 9)
                {
                    Session["NewbieTipStage"] = 10;
                    Session["NewbieTipText"] = GND.ReadNivdalStringData("TipId", "Language", "dbo.Nivdal_NewbieTipsGetTip", "10", ou.Language);
                    GND.ExecNivdalSQL("UserId", "StageId", "Nivdal_NewbieTipsSetStage", ou.UserId, "10");
                }
                
            }

            SuggestionsPanel.Visible = (this.SuggestionsGridView.Rows.Count > 0);
            MySuggestionsPanel.Visible = (this.MySuggestionsGridView.Rows.Count > 0);
        }

        protected string StopFriendshipOnClientClick()
        {
            return ("return confirm ('" + Resources.Resource.Friends_AreYouSureDeleteFriendshipQuestion + "')");
        }
        protected string DeclineFriendshipConfirm()
        {
            return ("return confirm ('" + Resources.Resource.Friends_AreYouSureDeclineFriendshipQuestion + "')");
        }
        protected string DeclineFriendshipBlacklistConfirm()
        {
            return ("return confirm ('" + Resources.Resource.Friends_AreYouSureBlacklistFriendshipQuestion + "')");
        }
        protected void DeleteFriend(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                string SuggestionId = e.CommandArgument.ToString();
                GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
                OnlineUser ou = (OnlineUser)Session["User"];
                string Answer = GND.ReadNivdalStringData("SuggestionId", "Language", "Nivdal_FriendshipStopBeingFriends", SuggestionId, ou.Language);
                Response.Redirect("~/Friends.aspx?Message=" + Answer);
            }
        }

        protected void FriendshipCommand(object sender, GridViewCommandEventArgs e)
        {
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            string Answer = "";

            if (e.CommandName == "Accept")
            {
                Answer = GND.ReadNivdalStringData("SuggestionId", "Nivdal_FriendshipApproveFriendshipSuggestion", e.CommandArgument);
            }
            else if (e.CommandName == "Decline")
            {
                Answer = GND.ReadNivdalStringData("SuggestionId", "@Blacklist", "Nivdal_FriendshipDeclineMembershipSuggestion", e.CommandArgument, false);
            }
            else if (e.CommandName == "Blacklist")
            {
                Answer = GND.ReadNivdalStringData("SuggestionId", "@Blacklist", "Nivdal_FriendshipDeclineMembershipSuggestion", e.CommandArgument, true);
            }

            //Response.Redirect("Friends.aspx?Message=" + Answer);
            SuggestionsGridView.DataBind();
            MessagePanel.Visible = true;
            MessageLabel.Text = Answer;
        }

        protected void MySuggestionsCommand(object sender, GridViewCommandEventArgs e)
        {
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"];
            string Answer = "";
            if (e.CommandName == "DeleteSuggestion")
            {
                Answer = GND.ReadNivdalStringData("SuggestionId", "Language", "Nivdal_FriendshipStopBeingFriends", e.CommandArgument.ToString(), ou.Language);
                //Response.Redirect("Friends.aspx?Message=" + Answer);
                MessagePanel.Visible = true;
                MessageLabel.Text = Answer;
                MySuggestionsGridView.DataBind();
            }
            
        }

        protected void DeleteSelected(object sender, EventArgs e)
        {
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"];
            string Answer = "";
            foreach (GridViewRow r in MySuggestionsGridView.Rows)
            {
                if ((r.FindControl("ToDeleteCheckBox") as CheckBox).Checked)
                {
                    string SuggestionId = (r.FindControl("SuggestionIdHiddenField") as HiddenField).Value;
                    Answer = GND.ReadNivdalStringData("SuggestionId", "Language", "Nivdal_FriendshipStopBeingFriends", SuggestionId, ou.Language);
                }
            }
            Response.Redirect("Friends.aspx?Message=" + Answer);
        }

        protected override void InitializeCulture()
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(ou.UICulture);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ou.Culture);
        }

       

       
      
    }
}